#include "../api/base.h"
#include "../api/ex_math.h"
#include "./object.h"

//创建obj
sObj*sNewObj(int x,int y,sSpr*spr)
{
 sObj*obj = malloc(sizeof(sObj));
 obj->x = x;
 obj->y = y;
 obj->id = 0;
 
 obj->spr = spr;
 obj->sprw = 0;
 obj->sprh = 0;
 if(obj->spr!=NULL)
 {
  obj->sprw = obj->spr->w;
  obj->sprh = obj->spr->h;
 }
 
 obj->mask = spr;//碰撞盒
 obj->mask_show = FALSE;
 obj->act = 0;
 
 obj->img = 0;//图片帧
 obj->img_sp = 100;
 obj->_img_tim = getuptime();
 
 obj->xsca = FALSE;
 obj->ysca = FALSE;
 obj->view = TRUE;
 
 obj->sp = 0;//运动
 obj->dir = 0;
 
 obj->step = NULL;
 obj->coll = NULL;
 obj->draw = NULL;
 obj->eve = NULL;
 
 for(int i = 0;i<10;i++)
 	obj->data[i] = -1;
 return obj;
}

//设置分组
void sSetObjId(sObj*obj,int id)
{
 if(obj==NULL)
 	return;
 obj->id = id;
}

//运行obj
void sRunObj(sObj*obj)
{
 if(obj==NULL)
 	return;
 if(getuptime()-obj->_img_tim>
   obj->img_sp)
 {
  if(obj->spr!=NULL)
  {
   sSetActId(obj->spr,
    obj->act);
   sSetImgId(obj->spr,
    obj->img);
   sRunSpr(obj->spr);
   obj->img = obj->spr->
   act[obj->spr->ind].ind;
  }
  if(obj->step!=NULL)
  {
   (*obj->step)(obj);
  }
  obj->_img_tim = getuptime();
 }
 //obj->act=obj->spr->ind;
}

//刷新精灵数据
void sRefObjSpr(sObj*obj)
{
 if(obj->spr==NULL)
 	return;
 obj->spr->x = obj->sprw*
 obj->spr->x/obj->spr->w;
 obj->spr->y = obj->sprh*
 obj->spr->y/obj->spr->h;
 obj->spr->w = obj->sprw;
 obj->spr->h = obj->sprh;
}

void sDrawObjPos(sObj*obj,int x,int y)
{
 //记录改变
 int dx,dy,w,h;
 
 if(obj==NULL)
 	return;
 if(obj->view)
 	if(obj->spr!=NULL)
 {
  sSetActId(obj->spr,obj->act);
  sSetImgId(obj->spr,obj->img);
  
  dx = obj->spr->x;
  dy = obj->spr->y;
  w = obj->spr->w;
  h = obj->spr->h;
  sRefObjSpr(obj);
  sDrawSpr(obj->spr,x,y);
  obj->spr->x = dx;
  obj->spr->y = dy;
  obj->spr->w = w;
  obj->spr->h = h;
  
  if(obj->mask!=NULL)
  	if(obj->mask_show)
  	sDrawSpr(obj->mask,x,y);
 }
}

//绘制obj
void sDrawObj(sObj*obj)
{
 sDrawObjPos(obj,obj->x,obj->y);
}

void sDrawObjCam(sObj*obj,sCam*cam)
{
 sDrawObjPos(obj,
  obj->x-cam->x,obj->y-cam->y);
}

//碰撞检测,不检测碰撞组
int sObjCollObj(sObj*obj1,sObj*obj2)
{
 sSpr*spr1 = obj1->mask;
 sSpr*spr2 = obj2->mask;
 if(spr1==NULL||spr2==NULL)
 	return isCollRect(obj1->x,obj1->y,
  obj1->sprw,obj1->sprh,
  obj2->x,obj2->y,
  obj2->sprw,obj2->sprh);
 
 int dx1 = obj1->spr->x;
 int dy1 = obj1->spr->y;
 int w1 = obj1->spr->w;
 int h1 = obj1->spr->h;
 sRefObjSpr(obj1);
 int dx2 = obj2->spr->x;
 int dy2 = obj2->spr->y;
 int w2 = obj2->spr->w;
 int h2 = obj2->spr->h;
 sRefObjSpr(obj2);
 
 int x1 = obj1->x-spr1->x;
 int y1 = obj1->y-spr1->y;
 int x2 = obj2->x-spr2->x;
 int y2 = obj2->y-spr2->y;
 
 int result = isCollRect(x1,y1,
  spr1->w,spr1->h,
  x2,y2,spr2->w,spr2->h);
 
 obj1->spr->x = dx1;
 obj1->spr->y = dy1;
 obj1->spr->w = w1;
 obj1->spr->h = h1;
 
 obj2->spr->x = dx2;
 obj2->spr->y = dy2;
 obj2->spr->w = w2;
 obj2->spr->h = h2;
 
 return result;
}

//点与obj碰撞
int sPosCollObj(int x,int y,sObj*obj)
{
 sSpr*spr = obj->mask;
 if(spr==NULL)
 	return isPointCollRect(x,y,obj->x,
  obj->y,obj->sprw,obj->sprh);
 
 int dx = obj->spr->x;
 int dy = obj->spr->y;
 int w = obj->spr->w;
 int h = obj->spr->h;
 sRefObjSpr(obj);
 
 int x1 = obj->x-spr->x;
 int y1 = obj->y-spr->y;
 
 int result = isPointCollRect(x,y,
  x1,y1,spr->w,spr->h);
 
 obj->spr->x = dx;
 obj->spr->y = dy;
 obj->spr->w = w;
 obj->spr->h = h;
 
 return result;
}

//矩形与obj碰撞
int sRectCollObj(int x,int y,int w,int h,sObj*obj)
{
 sSpr*spr = obj->mask;
 if(spr==NULL)
 	return isCollRect(x,y,w,h,obj->x,
  obj->y,obj->sprw,obj->sprh);
 
 int dx = obj->spr->x;
 int dy = obj->spr->y;
 int dw = obj->spr->w;
 int dh = obj->spr->h;
 sRefObjSpr(obj);
 
 int x1 = obj->x-spr->x;
 int y1 = obj->y-spr->y;
 
 int result = isCollRect(x,y,w,h,
  x1,y1,spr->w,spr->h);
 
 obj->spr->x = dx;
 obj->spr->y = dy;
 obj->spr->w = dw;
 obj->spr->h = dh;
 
 return result;
}

//释放obj
void sFreeObj(sObj*obj)
{
 free(obj);
}

